package com.example.course.service.impl;

import com.example.course.bean.Teacher;
import com.example.course.mapper.TeacherMapper;
import com.example.course.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public Teacher getTeacher(Teacher teacher) {
        return teacherMapper.selectTeacher(teacher);
    }

    @Override
    public List<Teacher> getTeachers() {
        return teacherMapper.selectTeachers();
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        return teacherMapper.updateTeacher(teacher);
    }

    @Override
    public int deleteTeacher(int id) {
        int i = teacherMapper.deleteTeacher(id);
        if (i == 1) {
            if (teacherMapper.selectTeachersByTeacherId(id) != null && teacherMapper.selectStudentsByTeacherId(id)!=null) {
                int i1 = teacherMapper.deleteCourseStudent1(id);
                int i2 = teacherMapper.deleteCourseTeacher1(id);
                if (i1 != 1 && i2 != 1) {
                    return 1;
                }else {
                    return 0;
                }
            }else if (teacherMapper.selectTeachersByTeacherId(id) != null){
                int i1 = teacherMapper.deleteCourseTeacher1(id);
                if (i1 != 1) {
                    return 1;
                }else {
                    return 0;
                }
            }else if (teacherMapper.selectStudentsByTeacherId(id)!=null) {
                int i2 = teacherMapper.deleteCourseStudent1(id);
                if (i2 != 1) {
                    return 1;
                }else {
                    return 0;
                }
            }else {
                return i;
            }
        }
        return 0;
    }

    @Override
    public int insertTeacher(Teacher teacher) {
        Teacher teacher1 = teacherMapper.selectTeacherByAccount(teacher.getAccount());
        if (teacher1 != null) {
            return 0;
        }
        return teacherMapper.insertTeacher(teacher);
    }

    @Override
    public Teacher getTeacherById(int id) {
        return teacherMapper.selectTeacherByTeacherId(id);
    }

    @Override
    public Teacher getTeacherByCourseId(int courseId) {
        return teacherMapper.selectTeacherByCourseId(courseId);
    }


}
